package utils;

import java.io.File;  
import java.io.IOException;
import java.io.FileInputStream;  
import java.io.InputStream;  

public class TiffUtil
{

private  Long iWidth = 0L;
private  Long iHeight = 0L;
private  Boolean fileexist = false;
private  Boolean istiff = false;

public  String HexString( byte[] b,String flag) { 
  String result="";
     for (int i = 0; i < b.length; i++) { 
       String hex = Integer.toHexString(b[i] & 0xFF); 
       if (hex.length() == 1) { 
         hex = '0' + hex; 
       } 
       if (flag.equals("II")){
          result=hex.toUpperCase()+result;  //小字节  高位在后，低位在前
       }else{
        	result= result + hex.toUpperCase();  //大字节  高位在前，低位在后
       }
     } 
     return result;
 }


  public  void ifdParse(String filename) throws IOException
  {
            File f = new File(filename);    

            if (f.exists())    
            {    
               fileexist = true;

               byte[] flags = new byte[2];
               byte[] ifh = new byte[4];
               InputStream in = new FileInputStream(filename);
               in.read(flags);
               
               if ((new String(flags).equals("II")) || (new String(flags).equals("MM"))){
               	istiff = true;
               	}
               
               if (istiff == false) return;	
               
               in.skip(2);
               in.read(ifh);

               String Xoffset = HexString(ifh,new String(flags));
               Long Offset = Long.valueOf(Xoffset,16);
               
               in.skip(Offset - 8);
               
               byte[] ifds = new byte[2];
               in.read(ifds);
               
               int ifdcount = Integer.valueOf(HexString(ifds,new String(flags)),16);
               for (int i=0;i<ifdcount;i++)
               {
               	byte[] btag = new byte[2];
               	in.read(btag);
                in.skip(6);
               	
                byte[] valueOffset = new byte[4];
                in.read(valueOffset);
                if ( HexString(btag,new String(flags)).equals("0100")){
                	 iWidth = Long.valueOf(HexString(valueOffset,new String(flags)),16);
                	}
                	
                if ( HexString(btag,new String(flags)).equals("0101")){
                	 iHeight = Long.valueOf(HexString(valueOffset,new String(flags)),16);
                	}	
                	
               	
               	}
               

            }  
  	
  	}

  public  Long getWidth()
  {
  	 return iWidth;
  }
  
  public  Long getHeight()
  {
  	 return iHeight;
  }
  
  public  Boolean getFileexist()
  {
  	 return fileexist;
  }
  
  public  Boolean getIstiff()
  {
  	 return istiff;
  }
  
	public static void main(String[] args) throws IOException
	{

            String filename = args[0];
            TiffUtil tiff = new TiffUtil();
            tiff.ifdParse(filename); 
            if (tiff.getFileexist() == false){
              System.out.println("文件不存在");
              return;
            }
            
            if (tiff.getIstiff()){
              System.out.println("是Tiff文件");
              System.out.println("宽度："+tiff.getWidth().toString());   
              System.out.println("高度："+tiff.getHeight().toString());   
            }else{
              System.out.println("不是Tiff文件");
            }
          
  }

}

